package cn.kgc.crowdfundingadminspringboot2.controller;

import cn.kgc.crowdfundingadminspringboot2.common.CrowdFundingConstant;
import cn.kgc.crowdfundingadminspringboot2.common.ResultEntity;
import cn.kgc.crowdfundingadminspringboot2.pojo.Admin;
import cn.kgc.crowdfundingadminspringboot2.service.AdminService;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Resource
    private AdminService adminService;


    @RequestMapping("/test")
    @ResponseBody
    public Admin selectById(Integer id){
        return adminService.selectById(id);
    }

    /**
     * 跳转登录页面
     * @return
     */
    @RequestMapping("/to/login/page.html")
    public String toLoginPage(){
        return "admin-login";
    }


    /**
     * 登录功能
     * @param loginAcct
     * @param userPswd
     * @param session
     * @param model
     * @return
     */
    @RequestMapping("/do/login.html")
    public String doLogin(@RequestParam("loginAcct") String loginAcct,
                          @RequestParam("userPswd") String userPswd,
                          HttpSession session,
                          Model model){
        Admin login = adminService.login(loginAcct, userPswd);
        if (login==null){
            model.addAttribute(CrowdFundingConstant.ATTR_NAME_MESSAGE, CrowdFundingConstant.MESSAGE_LOGIN_FAILED);
            return "admin-login";
        }
        session.setAttribute(CrowdFundingConstant.ATTR_NAME_LOGIN_ADMIN, login);
        return "redirect:/admin/to/main/page.html";

    }


    /**
     * 跳转后台主页面
     * @return
     */
    @RequestMapping("/to/main/page.html")
    public String toLogin(){
        return "admin-main";
    }


    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param keyword
     * @param model
     * @return
     */
    @RequestMapping("/query/for/search")
    public String queryForSearch(

            // 如果页面上没有提供对应的请求参数，那么可以使用defaultValue指定默认值
            @RequestParam(value="pageNum", defaultValue="1",required = false) Integer pageNum,
            @RequestParam(value="pageSize", defaultValue="5",required = false) Integer pageSize,
            @RequestParam(value="keyword", defaultValue="",required = false) String keyword,
            Model model) {
        PageInfo<Admin> adminPageInfo = adminService.queryForKeywordSearch(pageNum, pageSize, keyword);
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_PAGE_INFO,adminPageInfo);
        return "admin-page";
    }


    /**
     * 删除用户
     * @param adminIdList
     * @return
     */
    @ResponseBody
    @RequestMapping("/admin/batch/remove")
    public ResultEntity<String> batchRemove(@RequestBody List<Integer> adminIdList) {
        try {

            adminService.batchRemove(adminIdList);

            return ResultEntity.successWithoutData();
        }catch(Exception e) {

            return ResultEntity.failed(null, e.getMessage());
        }
    }


    /**
     * 异常处理
     * @param exception
     * @param model
     * @return
     */
    @ExceptionHandler(value=Exception.class)
    public String catchException(Exception exception,Model model) {
        model.addAttribute(CrowdFundingConstant.ATTR_NAME_ERROR_MESSAGE, exception.getMessage());
        return "system-error";
    }


    /**
     * 退出功能
     * @param session
     * @return
     */
    @RequestMapping("/admin/logout")
    public String logout(HttpSession session) {
        session.invalidate();
        return "redirect:/index";
    }


    /**
     * 跳转admin添加页面
     * @return
     */
    @RequestMapping("/to/add/page.html")
    public String toAdminAdd(){
        return "admin-add";
    }

    /**
     * 添加用户
     * @param admin
     * @return
     */
    @RequestMapping("/save")
    public String saveAdmin(Admin admin) {

        adminService.saveAdmin(admin);

        return "redirect:/admin/query/for/search";
    }

    /**
     * 跳转到修改页面并显示用户信息
     * @param adminId
     * @param model
     * @return
     */
    @RequestMapping("/to/edit/page")
    public String toEditPage(@RequestParam("adminId") Integer adminId, Model model) {

        Admin admin = adminService.getAdminById(adminId);

        model.addAttribute(CrowdFundingConstant.ATTR_ADMIN, admin);

        return "admin-edit";
    }

    /**
     * 修改
     * @param admin
     * @param pageNum
     * @return
     */
    @RequestMapping("/update")
    public String updateAdmin(Admin admin,Integer pageNum) {
        try {
            adminService.updateAdmin(admin);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "redirect:/admin/query/for/search?pageNum="+pageNum;
    }
}
